package controller;

import java.io.IOException;
import java.util.Locale;
import java.util.ResourceBundle;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import service.FromStackOverFlowBaby;
import service.MyTranslator;
import util.CacheHandle;

import model.QuestionModel;
import entity.Question;

/**
 * Servlet implementation class QuestionDetail
 */
public class QuestionDetail extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// Get question alias from uri
		String path = ((HttpServletRequest) request).getRequestURI();
		String[] splittedURI = path.split("/");
		String alias = splittedURI[3];
		String lang = splittedURI[1];

		// Language
		HttpSession session = request.getSession(true);
		ResourceBundle resource = null;
		resource = ResourceBundle.getBundle(lang, new Locale(lang));
		session.setAttribute("language", resource);

		// Return view
		QuestionModel questionModel = new QuestionModel();
		Question detail = questionModel.findByAlias(alias, lang);

		// Add cache
		if (detail != null) {
			CacheHandle.setQuestionCache(detail.getAlias());
		}

		if (detail != null && detail.getAlias() != "") {
			// Property handle
			request.setAttribute("title", detail.getTitle());
			request.setAttribute("keyword", resource.getString("keyword"));
			request.setAttribute("description",
					resource.getString("description"));
			request.setAttribute("detail", detail);
			request.getRequestDispatcher("/question_detail.jsp").forward(
					request, response);
		} else {
//			// translate if this question have no order language.
//			detail = questionModel.findByAlias(alias, "en");
//			if (detail != null && detail.isValid()) {
//				String title = detail.getTitle();
//				String question = detail.getContent();
//				detail = MyTranslator.translateFromDB(detail, "en", lang);
//				if (detail.getTitle().equalsIgnoreCase(title)
//						|| detail.getContent().equalsIgnoreCase(question)) {
//					System.out.println("cannot translate ??");
//					response.sendRedirect("/index");
//				} else {
//					questionModel.add(detail);
//					request.setAttribute("title", detail.getTitle());
//					request.setAttribute("keyword",
//							resource.getString("keyword"));
//					request.setAttribute("description",
//							resource.getString("description"));
//					request.setAttribute("detail", detail);
//					request.getRequestDispatcher("/question_detail.jsp")
//							.forward(request, response);
//				}
//			} else {
				// if have not this question, get from stack over flow.
//				FromStackOverFlowBaby dt = new FromStackOverFlowBaby();
//				detail = dt.searchAndGetQuestion(alias);
//				if (detail != null) {
//					request.setAttribute("title", detail.getTitle());
//					request.setAttribute("keyword",
//							resource.getString("keyword"));
//					request.setAttribute("description",
//							resource.getString("description"));
//					request.setAttribute("detail", detail);
//					request.getRequestDispatcher("/question_detail.jsp")
//							.forward(request, response);
//				} else {
//					System.out.println("cannot get question !");
//					response.sendRedirect("/index");
//				}
				response.sendRedirect("/index");
//			}
		}
	}
}
